package com.baidu.health.service.impl;

import com.baidu.health.mapper.*;
import com.baidu.health.pojo.*;
import com.baidu.health.service.TbUserEvaluatingService;
import com.baidu.health.vo.UserEvaluating;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class TbUserEvaluatingServiceImpl extends ServiceImpl<TbUserEvaluatingMapper, TbUserEvaluating>
    implements TbUserEvaluatingService{
    @Autowired
    private TbUserEvaluatingMapper tbUserEvaluatingMapper;
    @Autowired
    private TbQuestionnaireMapper tbQuestionnaireMapper;
    @Autowired
    private TbQuestionnaireDetailMapper tbQuestionnaireDetailMapper;
    @Autowired
    private TbUserEvaluatingRecordMapper tbUserEvaluatingRecordMapper;
    @Autowired
    private TbAskMapper tbAskMapper;
    @Autowired
    private TbAnswerMapper tbAnswerMapper;
    @Autowired
    private TbFactorMapper tbFactorMapper;
    @Autowired
    private TbAilmentFatorMapper tbAilmentFatorMapper;
    @Autowired
    private TbAilmentMapper tbAilmentMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public ResultResponse UserEvaluatingSelect(TbUserEvaluating tbUserEvaluating) {
        Integer userId = tbUserEvaluating.getUserId();
        QueryWrapper queryWrapper=new QueryWrapper();
        if(userId!=null){
            queryWrapper.eq("user_id",userId);
        }
        queryWrapper.eq("user_schedule",1);
        List<TbUserEvaluating> list = tbUserEvaluatingMapper.selectList(queryWrapper);
        for (int i = 0; i < list.size(); i++) {
            TbQuestionnaire tbQuestionnaire = tbQuestionnaireMapper.selectById(list.get(i).getQuestionnaireId());
            list.get(i).setQuestionnaireName(tbQuestionnaire.getQuestionnaireName());
        }
        return ResultResponse.SUCCESS(list);
    }

    @Override
    public ResultResponse UserEvaluatingDeleted(Integer evaluatingId) {
        tbUserEvaluatingMapper.deleteById(evaluatingId);
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse beginreview(TbUserEvaluating tbUserEvaluating) {
        tbUserEvaluating.setCreateTime(new Date());
        tbUserEvaluating.setUserSchedule((byte) 0);
        tbUserEvaluatingMapper.insert(tbUserEvaluating);
        Integer questionnaireId = tbUserEvaluating.getQuestionnaireId();
        List<Integer> askids=tbQuestionnaireDetailMapper.getAskIdListByPaperId(questionnaireId);
        String key = "review_"+tbUserEvaluating.getEvaluatingId();
        askids.forEach(item->{
            redisTemplate.opsForList().leftPush(key,item);
        });
        return ResultResponse.SUCCESS(tbUserEvaluating);
    }

    @Override
    public ResultResponse beginreviewSelect(TbUserEvaluating tbUserEvaluating) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("user_id",tbUserEvaluating.getUserId());
        queryWrapper.eq("questionnaire_id",tbUserEvaluating.getQuestionnaireId());
        queryWrapper.orderByDesc("create_time");
        TbUserEvaluating tb=null;
        List<TbUserEvaluating> list = tbUserEvaluatingMapper.selectList(queryWrapper);
        if(list!=null){
            for (int i = 0; i < list.size(); i++) {
                if(i==0){
                    tb=list.get(i);
                }
            }
            return ResultResponse.SUCCESS(tb);
        }else{
         return ResultResponse.SUCCESS();
        }
    }

    @Override
    public ResultResponse getEvaluationAsk(Integer revoewId) {
        String key = "review_"+revoewId;
        //获取到了问题id
        Integer askId = (Integer)redisTemplate.opsForList().rightPop(key);
        if (askId==null){
            //没有问题了,评测结束了
            return ResultResponse.FAILED(600,"评测结束");
        }
        //发送评测问题
        TbAsk tbAsk = tbAskMapper.selectById(askId);
        if (tbAsk==null){
            //先检查评测问题是否存在，如果不存在，直接返回
            return ResultResponse.FAILED(-1,"评测问题不存在");
        }
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("ask_id",askId);
        List<TbAnswer> list = tbAnswerMapper.selectList(queryWrapper);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("ask", tbAsk);
        map.put("answer", list);
        return ResultResponse.SUCCESS(map);
    }

    @Override
    public ResultResponse getUserEvaluatingAsk(UserEvaluating userEvaluating) {
        TbUserEvaluatingRecord record=new TbUserEvaluatingRecord();
        record.setUserId(userEvaluating.getUserId());
        record.setAnswerId(userEvaluating.getAnswerId());
        record.setAskId(userEvaluating.getAskId());
        record.setEvaluatingId(userEvaluating.getEvaluatingId());
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("ask_id",userEvaluating.getAskId());
        queryWrapper.eq("answer_id",userEvaluating.getAnswerId());
        TbQuestionnaireDetail tbQuestionnaireDetail = tbQuestionnaireDetailMapper.selectOne(queryWrapper);
        record.setRecordGrade(tbQuestionnaireDetail.getDetailGrade());
        record.setCreateTime(new Date());
        tbUserEvaluatingRecordMapper.insert(record);
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse gettbuserEvaluatingInsert(Integer evaluatingId) {
        TbUserEvaluating tbUserEvaluating = tbUserEvaluatingMapper.selectById(evaluatingId);
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("evaluating_id",evaluatingId);
        List<TbUserEvaluatingRecord> list = tbUserEvaluatingRecordMapper.selectList(queryWrapper);
        Integer userGroude=0;
        Integer num=0;
        Integer su=0;
        for (int i = 0; i < list.size(); i++) {
            TbUserEvaluatingRecord tbUserEvaluatingRecord = list.get(i);
            QueryWrapper queryWrapper1=new QueryWrapper();
            queryWrapper1.eq("questionaire_id",tbUserEvaluating.getQuestionnaireId());
            queryWrapper1.eq("ask_id",tbUserEvaluatingRecord.getAskId());
            queryWrapper1.eq("answer_id",tbUserEvaluatingRecord.getAnswerId());
            TbQuestionnaireDetail tbQuestionnaireDetail = tbQuestionnaireDetailMapper.selectOne(queryWrapper1);
            userGroude+=tbQuestionnaireDetail.getDetailGrade();
            num++;
        }
        su=userGroude/num;
        tbUserEvaluating.setEvaluatingGrade(su);
        tbUserEvaluating.setUserSchedule((byte) 1);
        tbUserEvaluating.setEvaluatingRemark("身体整体不错");
        tbUserEvaluating.setCreateTime(new Date());
        tbUserEvaluatingMapper.updateById(tbUserEvaluating);
        return ResultResponse.SUCCESS(tbUserEvaluating.getEvaluatingId());
    }

    @Override
    public ResultResponse getEvaluatingdetails(Integer evaluatingId) {
        TbUserEvaluating tbUserEvaluating = tbUserEvaluatingMapper.selectById(evaluatingId);
        TbQuestionnaire tbQuestionnaire = tbQuestionnaireMapper.selectById(tbUserEvaluating.getQuestionnaireId());
        tbUserEvaluating.setQuestionnaireName(tbQuestionnaire.getQuestionnaireName());
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("evaluating_id",evaluatingId);
        List<String> lists=new ArrayList<>();
        List<TbUserEvaluatingRecord> list = tbUserEvaluatingRecordMapper.selectList(queryWrapper);
        for (int i = 0; i < list.size(); i++) {
            Integer answerId = list.get(i).getAnswerId();
            TbAnswer tbAnswer = tbAnswerMapper.selectById(answerId);
            Integer factorId = tbAnswer.getFactorId();
            QueryWrapper queryWrapper1=new QueryWrapper();
            queryWrapper1.eq("factor_Id",factorId);
            List<TbAilmentFator> list1 = tbAilmentFatorMapper.selectList(queryWrapper1);
            for (int i1 = 0; i1 < list1.size(); i1++) {
                Integer ailmentId = list1.get(i1).getAilmentId();
                TbAilment tbAilment = tbAilmentMapper.selectById(ailmentId);
                lists.add(tbAilment.getAilmentName());
            }
        }
        List<String> list1=lists.stream().distinct().collect(Collectors.toList());
        tbUserEvaluating.setList(list1);
        return ResultResponse.SUCCESS(tbUserEvaluating);
    }
}